//导入axios包
import axios from 'axios'
import { Toast } from 'vant'

const request = axios.create({
  baseURL: 'http://111.230.181.206:3000',
  timeout: 5000
})

// 请求拦截
request.interceptors.request.use(config => {
  let token = localStorage.getItem('token')
  if(token){
    config.headers.Authorization = `${token}`;
  }
  return config
}, error => {
  // 出现异常
  return Promise.reject(error)
})

// 响应拦截
request.interceptors.response.use(response => {
  const { message, statusCode } = response.data
  if(message && statusCode == 401){
    Toast.fail(message)
  }
  if(message == '用户信息验证失败'){
    location.href = 'http://127.0.0.1:8080/#/login'
  }
  return response
}, error => {
  // 出现异常
  return Promise.reject(error)
})

//导出request
export default request
